package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatraintask;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 继续教育-师训任务-列表
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainTaskSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduTaskRepository yxyContinueduTaskRepository;

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @Autowired
    private YxyContinueduTaskLessonRepository yxyContinueduTaskLessonRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatraintask/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "endDate");
        Page<YxyContinueduTask> page = null;
        String userId = getProfile(request, response).getId();
        String taskStatus = reqModel.taskStatus;

        String reportStatus = reqModel.reportStatus;

        String keyword = reqModel.keyword;

        // 获取学校所在省的ID
        String areaIdProvince = getProfile(request, response).getSchoolAreaIdProvince();

        // 查询已经报名的所有任务
        List<YxyContinueduUserTask> userTasks = yxyContinueduUserTaskRepository.findByUserId(userId);
        List<String> idList = new ArrayList<String>();
        if (userTasks != null && userTasks.size() > 0) {
            // 取出任务Id
            for (YxyContinueduUserTask yxyContinueduUserTask : userTasks) {
                idList.add(yxyContinueduUserTask.getTaskId());
            }
        }

        // 任务为空
        if (taskStatus.isEmpty()) {
            // 报名情况为空
            if (reportStatus.isEmpty()) {
                // 关键词为空
                if (StringUtils.isBlank(reqModel.keyword)) {
                    page = yxyContinueduTaskRepository.findByAreaCodeProvinceAndStatusNot(areaIdProvince, -1, pageable);
                } else {
                    page = yxyContinueduTaskRepository
                            .findByAreaCodeProvinceAndNameContainingAndStatusNot(areaIdProvince, keyword, -1, pageable);
                }
            } else {
                // 报名情况不为空()
                if (reportStatus.equals("1")) {// 已报名
                    // 关键词为空
                    if (StringUtils.isBlank(reqModel.keyword)) {
                        page = yxyContinueduTaskRepository.findByIdInAndAreaCodeProvinceAndStatusNot(idList,
                                areaIdProvince, -1, pageable);
                    } else {
                        page = yxyContinueduTaskRepository.findByIdInAndAreaCodeProvinceAndNameContainingAndStatusNot(
                                idList, areaIdProvince, keyword, -1, pageable);
                    }
                } // 未报名
                else {
                    // 关键词为空
                    if (StringUtils.isBlank(reqModel.keyword)) {
                        page = yxyContinueduTaskRepository.findByIdNotInAndAreaCodeProvinceAndStatusNot(idList,
                                areaIdProvince, -1, pageable);
                    } else {
                        page = yxyContinueduTaskRepository
                                .findByIdNotInAndAreaCodeProvinceAndNameContainingAndStatusNot(idList, areaIdProvince,
                                        keyword, -1, pageable);
                    }

                }
            }

        } // 任务不为空
        else {
            // 进行中
            if (taskStatus.equals("1")) {
                // 报名情况为空
                if (reportStatus.isEmpty()) {
                    // 关键词为空
                    if (StringUtils.isBlank(reqModel.keyword)) {
                        page = yxyContinueduTaskRepository.findByAreaCodeProvinceAndStatus(areaIdProvince, 0, pageable);
                    } else {
                        page = yxyContinueduTaskRepository
                                .findByAreaCodeProvinceAndStatusAndNameContaining(areaIdProvince, 0, keyword, pageable);
                    }
                } else {
                    // 报名情况不为空()
                    if (reportStatus.equals("1")) {// 已报名
                        // 关键词为空
                        if (StringUtils.isBlank(reqModel.keyword)) {
                            page = yxyContinueduTaskRepository.findByIdInAndStatusAndAreaCodeProvince(idList, 0,
                                    areaIdProvince, pageable);
                        } else {
                            page = yxyContinueduTaskRepository.findByIdInAndStatusAndAreaCodeProvinceAndNameContaining(
                                    idList, 0, areaIdProvince, keyword, pageable);
                        }
                    } // 未报名
                    else {
                        // 关键词为空
                        if (StringUtils.isBlank(reqModel.keyword)) {
                            page = yxyContinueduTaskRepository.findByIdNotInAndStatusAndAreaCodeProvince(idList, 0,
                                    areaIdProvince, pageable);
                        } else {
                            page = yxyContinueduTaskRepository
                                    .findByIdNotInAndStatusAndAreaCodeProvinceAndNameContaining(idList, 0,
                                            areaIdProvince, keyword, pageable);
                        }

                    }
                }

            } // 已截止
            else {
                // 报名情况为空
                if (reportStatus.isEmpty()) {
                    // 关键词为空
                    if (StringUtils.isBlank(reqModel.keyword)) {
                        page = yxyContinueduTaskRepository.findByAreaCodeProvinceAndStatus(areaIdProvince, 1, pageable);
                    } else {
                        page = yxyContinueduTaskRepository
                                .findByAreaCodeProvinceAndStatusAndNameContaining(areaIdProvince, 1, keyword, pageable);
                    }
                } else {
                    // 报名情况不为空()
                    if (reportStatus.equals("1")) {// 已报名
                        // 关键词为空
                        if (StringUtils.isBlank(reqModel.keyword)) {
                            page = yxyContinueduTaskRepository.findByIdInAndStatusAndAreaCodeProvince(idList, 1,
                                    areaIdProvince, pageable);
                        } else {
                            page = yxyContinueduTaskRepository.findByIdInAndStatusAndAreaCodeProvinceAndNameContaining(
                                    idList, 1, areaIdProvince, keyword, pageable);
                        }
                    } // 未报名
                    else {
                        // 关键词为空
                        if (StringUtils.isBlank(reqModel.keyword)) {
                            page = yxyContinueduTaskRepository.findByIdNotInAndStatusAndAreaCodeProvince(idList, 1,
                                    areaIdProvince, pageable);
                        } else {
                            page = yxyContinueduTaskRepository
                                    .findByIdNotInAndStatusAndAreaCodeProvinceAndNameContaining(idList, 1,
                                            areaIdProvince, keyword, pageable);
                        }

                    }
                }

            }
        }
        List<RespModel> respModels = new ArrayList<RespModel>();
        int i = 1;
        if (page != null && page.getContent().size() > 0) {
            for (YxyContinueduTask yxyContinueduTask : page) {
                RespModel model = new RespModel();
                // 任务对应课程数量
                Integer countInteger = yxyContinueduTaskLessonRepository.countByTaskId(yxyContinueduTask.getId());
                model.taskId = yxyContinueduTask.getId();
                model.courseCount = countInteger;
                model.endDate = DateToolUtils.format(yxyContinueduTask.getEndDate(), DateToolUtils.FORMAT_SHORT_YMD);
                model.score = yxyContinueduTask.getScore();
                model.name = yxyContinueduTask.getName();
                model.num = i;

                // 判断任务状态 进行中
                if (yxyContinueduTask.getStatus() == 0) {
                    model.tStatus = "进行中";
                } // 已截止
                else if (yxyContinueduTask.getStatus() == 1) {
                    model.tStatus = "已截止";
                }

                // 报名情况
                if (idList.contains(yxyContinueduTask.getId())) {
                    model.rStatus = "已报名";
                } else {
                    model.rStatus = "未报名";
                }
                i++;
                respModels.add(model);
            }
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(respModels, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 任务状态 1 进行中 2已截止
        public String taskStatus;
        // 报名情况 1 已报名 2 未报名
        public String reportStatus;
        // 搜索
        public String keyword;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 任务ID
        public String taskId;
        // 当前任务序号
        public Integer num;
        // 任务名称
        public String name;
        // 学分数
        public Integer score;
        // 课程数
        public Integer courseCount;
        // 截止时间
        public String endDate;
        // 任务状态
        public String tStatus;
        // 报名情况
        public String rStatus;

    }
}
